
import Response from './response';
import {createRequestProxy} from "./proxy";
class Request{
    url = null;
    params = null;
    headers = {};
    token = null;
    interceptor = {};
    timeout = 30000;
    config = {};
    method = 'post';
    responseType = 'json';
    constructor(url, params, config) {
        this.url = url;
        this.params = params;
        this.config = config;
        if(config.headers) {
            this.headers = config.headers;
        } else {
            this.headers["Content-type"] = config.contentType;
        }
        if(this.config.timeout) {
            this.timeout = config.timeout;
        }
        return createRequestProxy(this, config);
    }

    setTimeout(timeout) {
        if(timeout) {
            this.timeout = timeout;
        }
    }

    setToken(token) {
        this.token = token;
    }

    setHeaders(headers = {}) {
        this.headers = headers;
    }

    addHeaders(key, value) {
        if(value) {
            this.headers[key] = value;
        }
    }

    setInterceptor(request, response) {
        if(request) {
            this.interceptor.request = request;
        }

        if(response) {
            this.interceptor.response = response;
        }
    }

    setResponseType(responseType) {
        this.responseType = responseType;
    }

    setMethod(method) {
        this.method = method;
    }

    send() {

        //todo 封装axios请求
        let config = {
            timeout: this.timeout,
            headers: this.headers,
            baseURL: "",
            responseType: this.responseType,
            method: this.method,
			data: this.params
        };

        if (this.url.indexOf('http://') === -1 && this.url.indexOf('https://') === -1) {
            if (this.config.baseUrl) {
                config.baseURL = this.config.baseUrl;
            }

            if (this.config.prefix) {
                config.baseURL += this.config.prefix;
            }

			config.baseURL = config.baseURL + this.url;
        }

        //todo 处理令牌
        if(this.token) {
            let authorizationPrefix = this.config.authorization.prefix;
            config.headers[this.config.authorization.key] = authorizationPrefix ? authorizationPrefix + this.token : this.token;
        }

		let interceptorRequest = this.config.interceptorRequest;
		interceptorRequest && interceptorRequest(config);

		if(this.interceptor){
		    this.interceptor.request && this.interceptor.request(config);
		}

		const success = (resolve, response) => {
			if(this.interceptor && this.interceptor.response){
			    this.interceptor.response(response);
			}
			resolve(response);
		}

		const fail = (reject, error) => {
			reject({
				code: error.errno ? error.errno : 500,
				status: 500,
				message: error.errMsg ? error.errMsg : '系统错误，错误码：' + error.errno,
				config: config,
                header: config.headers,
				data: error.data
			})
		}

		let promise = new Promise((resolve, reject) => {
			if(config.method == 'upload') {
				const {file, ...formData} = config.data;
				uni.uploadFile({
				    url: config.baseURL,
				    formData: formData,
				    filePath: file,
				    name: 'file',
					// dataType: config.responseType,
				    header: config.headers,
					success: (response) => {
						if(response && response.data) {
							response.data = JSON.parse(response.data);
						}
						success(resolve, response)
					},
					fail: (error) => {fail(reject, error)}
				});
			} else {
				uni.request({
					url: config.baseURL,
					method: config.method,
					data: config.data,
					header: config.headers,
					dataType: config.responseType,
					success: (response) => {success(resolve, response)},
					fail: (error) => {fail(reject, error)}
				})
			}

		});

        return new Response(promise, this.config);
    }


}

export default Request;
